package com.hgk.wanwan.chatactivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.hgk.common.common.ui.view.RippleView;
import com.hgk.wanwan.chatactivity.adapter.ListViewAdapter;
import com.hgk.wanwan.chatactivity.molde.MsgIfo;

import java.text.SimpleDateFormat;
import java.util.Date;

import width.hgk.wanwan.R;

public class ChatActivity2 extends Activity {

    private ListViewAdapter adapter = null;

    private ListView listview;
    private RippleView mRvLeft, mRvRight;
    private EditText et_meg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initView();
        adapter = new ListViewAdapter(this);
        listview.setAdapter(adapter);
    }


    private void initView() {
        listview = (ListView) findViewById(R.id.lv_chat_listview);
        et_meg = (EditText) findViewById(R.id.et_content_view_chat);
        mRvLeft = (RippleView) findViewById(R.id.ripv_btn_left);
        mRvRight = (RippleView) findViewById(R.id.ripv_btn_right);

       /* et_meg.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (hasFocus) {
                    imm.showSoftInput(getWindow().getDecorView(), InputMethodManager.SHOW_FORCED);
                } else {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }
            }
        });*/
//        et_meg.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
//                return true;
//            }
//        });
        mRvRight.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                String msg = et_meg.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {
                    adapter.addDataToAdapter(new MsgIfo(null, msg,getDate()));
                    adapter.notifyDataSetChanged();
                    listview.smoothScrollToPosition(listview.getCount() - 1);
                    et_meg.setText("");
                } else {
                    Toast.makeText(ChatActivity2.this, "内容不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mRvLeft.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                String msg = et_meg.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {
                    adapter.addDataToAdapter(new MsgIfo(msg, null,getDate()));
                    adapter.notifyDataSetChanged();
                    listview.smoothScrollToPosition(listview.getCount() - 1);
                    et_meg.setText("");
                } else {
                    Toast.makeText(ChatActivity2.this, "内容不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public static void launch(Context context) {
        context.startActivity(new Intent(context, ChatActivity2.class));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN){
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                if(hideInputMethod(this, v)) {
                    return true; //隐藏键盘时，其他控件不响应点击事件==》注释则不拦截点击事件
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    public static boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            v.getLocationInWindow(leftTop);
            int left = leftTop[0], top = leftTop[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    public static Boolean hideInputMethod(Context context, View v) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            return imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        return false;
    }

    //获取当前时间
    public String getDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
}
